/**
 * 
 */
package gates;

import gates.internals.NBaseTransistor;
import gates.internals.Transistor;

/**
 * @author denis
 * 
 */
public class Or extends Gate {
	static {
		filename = "simple_or.properties";
	}

	public Or(int defect) {
		this(false, false, defect);
	}

	public Or(boolean in_1, boolean in_2, int defect) {
		this(new GateInput(in_1), new GateInput(in_2), defect);
	}

	public Or(GateInput in_1, GateInput in_2, int defect) {
		super(in_1, in_2, defect);
	}

	protected void generateCircuit() {
		NBaseTransistor nbTransistor1 = new NBaseTransistor("1", defect);
		defect--;
		bat.addObserver(nbTransistor1.getCollector());
		input_1.addObserver(nbTransistor1.getBase());

		NBaseTransistor nbTransistor2 = new NBaseTransistor("2", defect);
		defect--;
		nbTransistor1.getEmitter().addObserver(nbTransistor2.getCollector());
		input_2.addObserver(nbTransistor2.getBase());

		Transistor transistor1 = new Transistor("3", defect);
		defect--;
		nbTransistor2.getEmitter().addObserver(transistor1.getCollector());
		input_1.addObserver(transistor1.getBase());
		gnd.addObserver(transistor1.getEmitter());

		Transistor transistor2 = new Transistor("4", defect);
		defect--;
		nbTransistor2.getEmitter().addObserver(transistor2.getCollector());
		input_2.addObserver(transistor2.getBase());
		gnd.addObserver(transistor2.getEmitter());

		NBaseTransistor nbTransistor3 = new NBaseTransistor("5", defect);
		defect--;
		bat.addObserver(nbTransistor3.getCollector());
		nbTransistor2.getEmitter().addObserver(nbTransistor3.getBase());
		transistor1.getCollector().addObserver(nbTransistor3.getBase());
		transistor2.getCollector().addObserver(nbTransistor3.getBase());

		Transistor transistor3 = new Transistor("6", defect);
		defect--;
		nbTransistor3.getEmitter().addObserver(transistor3.getCollector());
		nbTransistor2.getEmitter().addObserver(transistor3.getBase());
		transistor1.getCollector().addObserver(transistor3.getBase());
		transistor2.getCollector().addObserver(transistor3.getBase());
		gnd.addObserver(transistor3.getEmitter());

		nbTransistor3.getEmitter().addObserver(output);
		transistor3.getCollector().addObserver(output);
		
		components.add(nbTransistor1);
		components.add(nbTransistor2);
		components.add(nbTransistor3);
		components.add(transistor1);
		components.add(transistor2);
		components.add(transistor3);
	}

	public boolean getOutput() {
		return output.getValue();
	}

	public static void main(String... args) {
		boolean x = false, y = false;
		int def = 0;
		Or or = new Or(new GateInput(x), new GateInput(y), def);
		System.out.println(or.getOutput());
		System.out.println("x = " + x + "; y = " + y);
	}
}
